//
//  CameraViewController.m
//  植物管理
//
//  Created by liujianlong on 2022/11/22.
//
#import "AddViewController.h"

#import "CameraViewController.h"
@interface CameraViewController ()<CameraViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) IBOutlet UIImageView *resultimg;
@property (strong, nonatomic) IBOutlet UILabel *resultcontent;
@property (strong, nonatomic) IBOutlet CameraView *cameraView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bgviewtop;
@property (strong, nonatomic) IBOutlet UIButton *ablumBtn;
@property (strong, nonatomic) IBOutlet UIButton *lightBtn;
@property (strong, nonatomic) IBOutlet UIButton *tackBtn;
@property (strong, nonatomic)  NSURLSessionTask *task;
@property (nonatomic, strong) Job *model;
@end

@implementation CameraViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.task cancel];
    [CCHud stopHud];
    [self.cameraView.session stopRunning];
}
- (void)initUI{
    self.cameraView.delegate = self;
    self.cameraView.type = CameraViewTypeNormal;
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 201) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }else if (sender.tag == 202){///相册
        [self.cameraView openAlbum];
    }else if (sender.tag == 203){///拍照
        [self.cameraView takePhone];
    }else if (sender.tag == 204){
        sender.selected = !sender.selected;
        [self.cameraView flash:sender.selected];
    }else if (sender.tag == 205){
        AddViewController *vc = [AddViewController new];
       CCNavigationController *navc = [[CCNavigationController alloc] initWithRootViewController:vc];
        vc.model = self.model;
        navc.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self presentViewController:navc animated:false completion:nil];
    }else if (sender.tag == 206){
        [self.task cancel];
        [CCHud stopHud];
        self.bgView.hidden = YES;
        self.tackBtn.hidden = NO;
        self.ablumBtn.hidden = NO;
        self.lightBtn.hidden = NO;
        self.resultimg.image = nil;
        self.resultcontent.text = @"";
        [self.cameraView.session startRunning];
    }
}
- (Job *)model{
    if (!_model) {
        _model = [Job new];
    }
    return _model;
}
#pragma mark cameraviewdelegate
- (void)finished:(NSData *)imgData :(NSString *)resultString{
    UIImage *img = [UIImage imageWithData:imgData];
    self.bgView.hidden = NO;
    self.tackBtn.hidden = YES;
    self.ablumBtn.hidden = YES;
    self.lightBtn.hidden = YES;
    self.resultimg.image = img;
    [self.model.imgs addObject:imgData];
    self.model.account = APP.user.account;
    self.model.num = @"1";
    self.model.stamp = [[NSDate currentTimeStamp:@"yyyy/MM/dd HH:mm:ss"] integerValue];
    self.task = [SWTool swIMG:img type:ShiWuTypeOther success:^(NSDictionary * _Nonnull dict) {
        if ([dict isKindOfClass:[NSDictionary class]] &&
            [[dict valueForKey:@"result"] isKindOfClass:[NSArray class]]) {
            NSArray *array = dict[@"result"];
            NSDictionary *dc = array[0];
            self.model.plant = dc[@"keyword"];
            self.model.plantDetail = dc[@"root"];
            self.resultcontent.text = [NSString stringWithFormat:@"%@\n%@",dc[@"keyword"],dc[@"root"]];
        }else{
            self.model.plant = dict[@"keyword"];
            self.model.plantDetail = dict[@"root"];
            self.resultcontent.text = [NSString stringWithFormat:@"%@\n%@",dict[@"keyword"],dict[@"root"]];
        }
    }];
}
- (void)maskLayer:(cccameraMaskLayer *)maskLayer :(UIView *)scanView{
    maskLayer.maskColor = [@"#000000" color:0.8];
    scanView.frame = CGRectMake(50, 100, SCREEN_WIDTH - 100, SCREEN_WIDTH - 100);
    self.bgviewtop.constant = scanView.frame.origin.y + scanView.frame.size.height ;
}
- (BOOL)scanLine:(UIView *)scanLine :(CABasicAnimation *)animation{
    return  NO;
}
@end
